home *** CD-ROM | disk | FTP | other *** search
/ Collection of Internet / Collection of Internet.iso / infosrvr / dev / scott / WWW / NextStep / Implementation / old / HTString.c < prev    next >
C/C++ Source or Header  |  1992-11-23  |  2KB  |  87 lines

  1. /*        Case-independent string comparison        HTString.c
  2. **
  3. **    Original version came with listserv implementation.
  4. **    Version TBL Oct 91 replaces one which modified the strings.
  5. **    02-Dec-91 (JFG) Added stralloccopy and stralloccat
  6. **    23 Jan 92 (TBL) Changed strallocc* to 8 char HTSAC* for VM and suchlike
  7. **     6 Oct 92 (TBL) Moved WWW_TraceFlag in here to be in library
  8. */
  9. #include <ctype.h>
  10. #include "HTUtils.h"
  11. #include "tcp.h"
  12.  
  13. PUBLIC int WWW_TraceFlag = 0;    /* Global trace flag for ALL W3 code */
  14. PUBLIC CONST char * HTLibraryVersion = "nextfudge"; /* String for help screen etc */
  15.  
  16. #ifndef VM        /* VM has these already it seems */
  17.     
  18. /*    Strings of any length
  19. **    ---------------------
  20. */
  21. PUBLIC int strcasecomp ARGS2 (CONST char*,a, CONST char *,b)
  22. {
  23.     CONST char *p =a;
  24.     CONST char *q =b;
  25.     for(p=a, q=b; *p && *q; p++, q++) {
  26.         int diff = TOLOWER(*p) - TOLOWER(*q);
  27.         if (diff) return diff;
  28.     }
  29.     if (*p) return 1;    /* p was longer than q */
  30.     if (*q) return -1;    /* p was shorter than q */
  31.     return 0;        /* Exact match */
  32. }
  33.  
  34.  
  35. /*    With count limit
  36. **    ----------------
  37. */
  38. PUBLIC int strncasecomp ARGS3(CONST char*,a, CONST char *,b, int,n)
  39. {
  40.     CONST char *p =a;
  41.     CONST char *q =b;
  42.     
  43.     for(p=a, q=b;; p++, q++) {
  44.         int diff;
  45.         if (p == a+n) return 0;    /*   Match up to n characters */
  46.         if (!(*p && *q)) return *p - *q;
  47.         diff = TOLOWER(*p) - TOLOWER(*q);
  48.         if (diff) return diff;
  49.     }
  50.     /*NOTREACHED*/
  51. }
  52. #endif
  53.  
  54. /*    Allocate a new copy of a string, and returns it
  55. */
  56. PUBLIC char * HTSACopy
  57.   ARGS2 (char **,dest, CONST char *,src)
  58. {
  59.   if (*dest) free(*dest);
  60.   if (! src)
  61.     *dest = NULL;
  62.   else {
  63.     *dest = (char *) malloc (strlen(src) + 1);
  64.     if (*dest == NULL) outofmem(__FILE__, "HTSACopy");
  65.     strcpy (*dest, src);
  66.   }
  67.   return *dest;
  68. }
  69.  
  70. PUBLIC char * HTSACat
  71.   ARGS2 (char **,dest, CONST char *,src)
  72. {
  73.   if (src && *src) {
  74.     if (*dest) {
  75.       int length = strlen (*dest);
  76.       *dest = (char *) realloc (*dest, length + strlen(src) + 1);
  77.       if (*dest == NULL) outofmem(__FILE__, "HTSACat");
  78.       strcpy (*dest + length, src);
  79.     } else {
  80.       *dest = (char *) malloc (strlen(src) + 1);
  81.       if (*dest == NULL) outofmem(__FILE__, "HTSACat");
  82.       strcpy (*dest, src);
  83.     }
  84.   }
  85.   return *dest;
  86. }
  87.